﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;

#region StockInfo

public struct StockInfo : IBusinessEntity
{
    private string _pinCode;
    private string _branchName;

   
    StockInfo(string pinCode, string branchName)
    {
        _pinCode = pinCode;
        _branchName = branchName;
    }

    public string PinCode
    {
        get { return _pinCode; }
        set { _pinCode = value; }
    }
    public string BranchName
    {
        get { return _branchName; }
        set { _branchName = value; }
    }



    public void Fill(SqlDataReader reader)
    {
        if (DAL.ColumnExists(reader, "PinCode"))
            _pinCode = reader.GetString(0);
        if (DAL.ColumnExists(reader, "BranchName"))
            _branchName = reader.GetString(1);
    }


}
#endregion

/// <summary>
/// Summary description for DALBranch
/// </summary>
[System.ComponentModel.DataObject]
public class DALStock
{
    public static bool CheckDeliverableInStock(string DeliverableID,string pincode)
    {
        
        if (DAL.GetDataItems<Stock>(SQL.CheckDeliverableInStock, new string[] { "@DeliverableID", "@StockPinCode" }, new object[] { DeliverableID, pincode }).Count > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public static List<StockInfo> ListStock()
    {
        List<StockInfo> stockInfos=DAL.GetDataItems<StockInfo>(SQL.BranchInformation, new string[] { }, new object[] {  });
        StockInfo temp = new StockInfo();
        temp.PinCode = String.Empty;
        temp.BranchName = "NULL";
        stockInfos.Add(temp);
        return stockInfos;
    }
    public static bool CheckDeliveryNumber(string DeliverableID)
    {
        
        if (DAL.GetDataItems<Deliverable>(SQL.GetDeliverablesByDeliverableID, new string[] { "@DeliverableID" }, new object[] { DeliverableID }).Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// Search all the the deliverable on delivery number entered by employee
    /// </summary>
    /// <param name="DeliverableID">The delivery number</param>
    /// <returns>List of deliverable</returns>
    public static List<Stock> SearchDeliverableOnDeliveryNumber(string DeliverableID, string pincode)
    {
        
        DeliverableID = "%" + DeliverableID + "%";

        return DAL.GetDataItems<Stock>(SQL.SearchDeliverableOnDeliveryNumber, new string[] { "@DeliverableID","@StockPinCode" }, new object[] { DeliverableID, pincode });
    }

    /// <summary>
    /// Search all the the deliverable on pincode entered by employee
    /// </summary>
    /// <param name="pincode">The pincode of branch which employee belong to</param>
    /// <returns>List of deliverable</returns>
    public static List<Stock> SearchDeliverableOnPincode(string pincode,string StockPinCode)
    {
        pincode = "%" + pincode + "%";
        return DAL.GetDataItems<Stock>(SQL.SearchDeliverableOnPinCode, new string[] { "@FromStock", "@ToStock", "@StockPinCode" }, new object[] { pincode, pincode, StockPinCode });
    }

    /// <summary>
    /// Search delivery number based on delivery number
    /// </summary>
    /// <param name="DeliverableID">The delivery number</param>
    /// <returns>The list of delivery number similar to delivery number entered by employee</returns>
    public static List<String> SearchOnDeliverNumber(string DeliverableID, string StockPinCode)
    {
        
        DeliverableID = "%" + DeliverableID + "%";
        List<String> deliveryNumbers = new List<string>();
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("@DeliverableID",DeliverableID);
        cmd.Parameters.AddWithValue("@StockPinCode", StockPinCode);
        cmd.CommandText = SQL.SearchDeliveryNumber;
        SqlDataReader rd=DAL.ExecuteReader(cmd);
        while (rd.HasRows)
        {
           deliveryNumbers.Add(rd.GetInt32(0).ToString());
        }
        return deliveryNumbers;
    }

    /// <summary>
    /// Search deliverable based on pincode
    /// </summary>
    /// <param name="PinCode">The branch's pincode</param>
    /// <returns>The list of pincode similar to pincode entered by employee</returns>
    public static List<String> SearchOnPincode(string PinCode, string StockPinCode)
    {

        PinCode = "%" + PinCode + "%";
        List<String> pinCodes = new List<string>();

        //Get Data from FromStock
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("@FromStock",PinCode);
        cmd.Parameters.AddWithValue("@StockPinCode", StockPinCode);
        cmd.CommandText = SQL.SearchFromStock;
        SqlDataReader rd = DAL.ExecuteReader(cmd);
        while (rd.HasRows)
        {
            pinCodes.Add(rd.GetSqlString(0).ToString());
        }

        //Get Data from ToStock
        cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue( "@ToStock",PinCode);
        cmd.Parameters.AddWithValue("@StockPinCode", StockPinCode);
        cmd.CommandText = SQL.SearchToStock;
        rd = DAL.ExecuteReader(cmd);
        while (rd.HasRows)
        {
            pinCodes.Add(rd.GetSqlString(0).ToString());
        }
        return pinCodes;

    }

    /// <summary>
    /// The list of deliverable of stock
    /// </summary>
    /// <returns>The list of Stock entity</returns>
    public static List<Stock> ViewStock(string StockPinCode)
    {
        return DAL.GetDataItems<Stock>(SQL.ListStock, new string[] {"@StockPinCode" }, new object[] { StockPinCode });
    }

    /// <summary>
    /// The specify deliverable in stock based on deliverable id
    /// </summary>
    /// <param name="DeliverableID">The delivery number</param>
    /// <returns>Stock entity</returns>
    public static Stock SelectedDeliverableInStock(string DeliverableID, string StockPinCode)
    {
        
        return DAL.GetDataItem<Stock>(SQL.SelectedStock, new string[] { "@DeliverableID","@StockPinCode" }, new object[] { DeliverableID, StockPinCode });
    }
   

    /// <summary>
    /// Add information of deliverable
    /// </summary>
    /// <param name="FromStock">The Pincode of previous stock store deliverable before delivery</param>
    /// <param name="ToStock">The pincode of stock will store deliverable after delivery</param>
    /// <param name="ReceivedDate">Time received deliverable</param>
    /// <param name="SentDate">Time send deliverable</param>
    /// <param name="Status">Status is send or isn't send</param>
    /// <param name="DeliverableID">The delivery number</param>
    /// <returns>Add successful or not</returns>
    public static bool AddDeliveralbeInStock(string FromStock, string ToStock,
        string ReceivedDate, string SentDate, bool Status, string DeliverableID, string StockPinCode)
    {
        
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("@FromStock", FromStock);
        cmd.Parameters.AddWithValue("@ToStock", ToStock);
        cmd.Parameters.AddWithValue("@ReceivedDate", ReceivedDate);
        cmd.Parameters.AddWithValue("@SentDate", SentDate);
        cmd.Parameters.AddWithValue("@Status", Status);
        cmd.Parameters.AddWithValue("@DeliverableID", DeliverableID);
        cmd.Parameters.AddWithValue("@StockPinCode", StockPinCode);
        if (FromStock.Equals(String.Empty) && ToStock.Equals(String.Empty))
        {
            cmd.Parameters.RemoveAt("@FromStock");
            cmd.Parameters.RemoveAt("@ToStock");
            cmd.CommandText = SQL.AddStock;
        }
        else if (FromStock.Equals(String.Empty))
        {
            cmd.Parameters.RemoveAt("@FromStock");
            cmd.CommandText = SQL.AddStockWithToStock;
        }
        else if (ToStock.Equals(String.Empty))
        {
            cmd.Parameters.RemoveAt("@ToStock");
            cmd.CommandText = SQL.AddStockWithFromStock;
        }
        else if (SentDate.Equals(String.Empty))
        {
            cmd.Parameters.RemoveAt("@SentDate");
            cmd.CommandText = SQL.AddStockWithoutSentDate;
        }
        else
            cmd.CommandText = SQL.AddStockWithFromStockToStock;
        if (DAL.ExecuteNonQuery(cmd) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Delete deliverable in stock based on delivery number
    /// </summary>
    /// <param name="DeliverableID">The delivery number</param>
    /// <returns>Delete successful or not</returns>
    public static bool DeleteDeliverableInStock(string DeliverableID,string StockPinCode)
    {
        
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("DeliverableID", DeliverableID);
        cmd.Parameters.AddWithValue("@StockPinCode", StockPinCode);
        cmd.CommandText = SQL.DeleteSelectedDeliverable;
        if (DAL.ExecuteNonQuery(cmd) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Check if deliverableID Exist In Stock
    /// </summary>
    /// <param name="DeliverableID"></param>
    /// <param name="StockPinCode"></param>
    /// <returns>True : Continue</returns>
    /// <returns>False : Redirect to ViewStock</returns>
    public static bool CheckDeliverable(string DeliverableID, string StockPinCode)
    {
        if (DAL.GetDataItem<Stock>(SQL.SelectedStock, new string[] { "@DeliverableID", "@StockPinCode" }, new object[] { DeliverableID, StockPinCode }).DeliverableID == String.Empty)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

